<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Special{
	
	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	
	/**
	 * 获取分类名
	 *
	 * @param int $id
	 * @return string
	 */
	function get_category_name( $id )
	{
		$sql = "SELECT `name` FROM `special_category` WHERE `id` = $id";
		$result = $this->db->query( $sql, true );
		return $result['name'];
	}
	
	/**
	 * 获取专题数
	 *
	 * @param string $type
	 * @param int $id
	 * @return int
	 */
	function get_special_no( $type, $id )
	{
		if( $type == 'cg' )
		{
			return $this->db->count( 'special', array( 'category_id' => $id ) );
		}
		else 
		{
			return $this->db->count( 'special' );
		}
	}
	
	/**
	 * 获取专题列表
	 *
	 * @param int $start
	 * @param string $type
	 * @param int $id
	 * @return array
	 */
	function get_special_list( $start, $type, $id )
	{
		$type == 'cg'?$where = '`special`.`category_id` = '.$id:$where = '';
		$sql = "SELECT `special`.`id`, `special`.`name`, `special`.`read_times`, `special_category`.`name` AS `category` FROM `special` 
		LEFT JOIN `` ON `special`.`category_id` = `special_category`.`id` $where ORDER BY `id` DESC LIMIT $start, 20";
		$result = $this->db->query( $sql );
		$datas = array();
		foreach( $result as $v )
		{
			$datas[] = array(
					'id' => $v['id'],
					'name' => $v['name'],
					'read_times' => $v['read_times'],
					'article_no' => $this->db->count( 'article', array( 'special_id' => $v['id'] ) ),
					'category' => $v['category']
			);
		}
		return $datas;
	}
	
	/**
	 * 获取分类树
	 *
	 * @param bool $special_no
	 * @return array
	 */
	function get_category_tree( $special_no = false )
	{
		$category_tree = cache_read( 'special', 'category' );
		$datas = array();
		foreach( $category_tree as $v )
		{
			if( $special_no )
			{
				$datas[] = array(
						'id' => $v['id'],
						'name' => $v['name'],
						'order' => $v['order'],
						'special_no' => $this->db->count( 'special', array( 'category_id' => $$v['id'] ) )
				);
			}
			else 
			{
				$datas[] = array(
						'id' => $v['id'],
						'name' => $v['name'],
						'order' => $v['order']
				);
			}
		}
		return $datas;
	}
	
	/**
	 * 获取专题模板
	 *
	 * @return array
	 */
	function get_template()
	{
		$config = cache_read( 'config', 'sysconfig' );
		$sql = "SELECT `id`, `name` FROM `template` WHERE `style` = '{$config['site_style']}' AND `type` = 'special_temp'";
		$temp = $this->db->query( $sql );
		return $temp;
	}
	
	/**
	 * 添加专题
	 *
	 * @param array $datas
	 * @return bool
	 */
	function add_special( $datas )
	{
		return $this->insert( 'special', $datas )?true:false;
	}
	
	/**
	 * 添加专题分类
	 *
	 * @param array $datas
	 * @return bool
	 */
	function add_category( $datas )
	{
		return $this->insert( 'special_category', $datas )?true:false;
	}
	
	/**
	 * 获取专题详细
	 *
	 * @param int $id
	 * @return array
	 */
	function get_special( $id )
	{
		$sql = "SELECT ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.``, ``.`` FROM `` 
		LEFT JOIN `special_category` ON `special_category`.`id` = `special`.`category_id` WHERE `special`.`id` = $id";
		return $this->db->query( $sql, true );
	}
	
	/**
	 * 更新专题
	 *
	 * @param array $datas
	 * @param int $id
	 * @return bool
	 */
	function update_special( $datas, $id )
	{
		return $this->update( 'special', $datas, array( 'id' => $id ) )?true:false;
	}
	
	/**
	 * 获取专题分类
	 *
	 * @return array
	 */
	function get_category()
	{
		$sql = "SELECT ``, ``, ``, ``, ``, `` FROM `special_category` WHERE `id` = $id";
		return $this->db->query( $sql, true );
	}
	
	/**
	 * 更新专题分类
	 *
	 * @param array $datas
	 * @param int $id
	 * @return bool
	 */
	function update_category( $datas, $id )
	{
		return $this->update( 'special_category', $datas, array( 'id' => $id ) )?true:false;
	}
	
}